package com.briup.srms.base.web.controller;

import com.briup.srms.base.bean.BasePrivilege;
import com.briup.srms.base.service.IBasePrivilegeService;
import com.jacky.base.utils.Message;
import com.jacky.base.utils.MessageUtil;
import com.briup.srms.base.vm.PrivilegeTree;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: kindergarten
 * @description: 权限控制器
 * @author: jacky
 * @create: 2020-11-16 15:51
 **/
@Api(description="权限相关接口")
@Validated
@RestController
@RequestMapping("/privilege")
public class PrivilegeController {

    @Autowired
    private IBasePrivilegeService basePrivilegeService;

    // √
    @ApiOperation(value = "查询所有")
    @GetMapping(value = "findAll")
    public Message findAll() {
        List<BasePrivilege> list = basePrivilegeService.findAll();
        return MessageUtil.success(list);
    }

    @ApiOperation(value = "根据ID删除")
    @DeleteMapping(value = "deleteById")
    @ApiImplicitParams(@ApiImplicitParam(name="id",value="编号",paramType="query",required=true))
    public Message deleteById(long id){
        basePrivilegeService.deleteById(id);
        return MessageUtil.success("删除成功");
    }

    // 暂未用到
    @ApiOperation(value = "通过parentId查询")
    @GetMapping(value = "findByParentId")
    public Message findByParentId(Long id){
        List<BasePrivilege> list = basePrivilegeService.findByParentId(id);
        return MessageUtil.success(list);
    }

    @ApiOperation(value ="保存或更新")
    @PostMapping(value = "saveOrUpdate")
    public Message saveOrUpdate(BasePrivilege privilege){
        basePrivilegeService.saveOrUpdate(privilege);
        return MessageUtil.success("更新成功");
    }

    // 查询权限树（所有父级+子级权限）
    @ApiOperation(value = "查询树")
    @GetMapping(value = "findPrivilegeTree")
    public Message findPrivilegeTree(){
        List<PrivilegeTree> list = basePrivilegeService.findPrivilegeTree();
        return MessageUtil.success(list);
    }



}
